package ${model.packageName}.${model.modulePackage}.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.util.StringUtils;

import com.apa.biz.common.CustomJdbcTemplate;
import com.apa.biz.common.CustomRowBounds;

import ${model.packageName}.${model.modulePackage}.entity.${model.model}Entity; 
 



/**
* ${model.model}DaoImpl: ${model.comment} 数据持久层接口实现
* @author ${model.author} ${date}
*
*/
public class ${model.model}DaoImpl {
	private Logger logger = LoggerFactory.getLogger(${model.model}DaoImpl.class);

	@Autowired
	CustomJdbcTemplate jdbcTemplate;
	
	// base column
	private static final String BASE_COLUMN = "SELECT "+
#foreach($field in $fieldList)
	#if($!{velocityCount}==$!{fieldList.size()})
	#if(${field.columnName}=='create_code')
	"cu1.`name` AS create_name";
	#elseif(${field.columnName}=='update_code')
	"cu2.`name` AS update_name ";
	#elseif(${field.columnName}!='update_code'&&${field.columnName}!='create_code')
	" s.${field.columnName}";
	#end
	#else
	#if(${field.columnName}=='create_code')
	"cu1.`name` AS create_name,"+
	#elseif(${field.columnName}=='update_code')
	"cu2.`name` AS update_name,"+
	#elseif(${field.columnName}!='update_code'&&${field.columnName}!='create_code')
	" s.${field.columnName},"+
	#end
	#end 
#end
	
	// base from
	private static final String BASE_FROM = " from ${model.tableName} s  LEFT JOIN cb_user cu1 ON s.create_code=cu1.`code` LEFT JOIN cb_user cu2 ON s.create_code=cu2.`code`  WHERE 1=1";

	// list
	private static final String SQL_LIST = BASE_COLUMN + BASE_FROM;
	
	// count
	private static final String SQL_COUNT = " SELECT count(DISTINCT s.code)" + BASE_FROM;
	
	// sum
	private static final String SQL_SUM = " SELECT count(DISTINCT s.code) "+ BASE_FROM;

	public Integer selectCount(Map<String, Object> params) {
		StringBuffer sql = new StringBuffer(SQL_COUNT);
		// 拼接查询条件
		pageWhereSql(sql, params);
		return jdbcTemplate.queryForObject(sql.toString(), java.lang.Integer.class);
	}

	public List<Map<String, Object>> selectListPage(Map<String, Object> params, CustomRowBounds rowBounds) {
		StringBuffer sql = new StringBuffer(SQL_LIST);
		// 拼接查询条件
		pageWhereSql(sql, params);
		logger.info("sql:" + sql);
		// 默认ID倒序排列
		sql.append(" ORDER BY " + rowBounds.getColumns() + " " + rowBounds.getDirection());
		// 分页
		int m = (rowBounds.getPageNumber() - 1) * rowBounds.getPageSize();
		int n = rowBounds.getPageSize();
		sql.append(" limit " + m + "," + n + " ");
		System.out.println(sql.toString());
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql.toString());
		return list;
	}
	
	public List<Map<String, Object>> selectSum(Map<String, Object> params) {
		StringBuffer sql = new StringBuffer(SQL_SUM);
		// 拼接查询条件
		pageWhereSql(sql, params);
		return jdbcTemplate.queryForList(sql.toString());
	}

	/**
	 * 组装查询条件
	 * 
	 * @param sql
	 * @param params
	 */
	private void pageWhereSql(StringBuffer sql, Map<String, Object> params) {
		// 过滤逻辑删除
		sql.append(" AND s.del_flag = 0");
		 	
#foreach($field in $fieldList)
#if(${field.field}!="code"&&${field.inputType}!="hidden"&&${field.field}!="delFlag")
		String ${field.field}=(String) params.get("${field.field}");
		if(StringUtils.hasText(${field.field})){
#if($field.matchType == 'any')
		sql.append(" and s.${field.columnName} like '%").append(${field.field}).append("%'");
#elseif($field.matchType == 'left')
		sql.append(" and s.${field.columnName} like '").append(${field.field}).append("%'");
#elseif($field.matchType == 'right')
		sql.append(" and s.${field.columnName} like '%").append(${field.field}).append("'");
#else
		sql.append(" and s.${field.columnName} = '").append(${field.field}).append("' ");
#end
}
#end
#end
		
	// create_time   
		String search_beginDate = (String) params.get("search_beginDate");
		if (!StringUtils.isEmpty(search_beginDate)) {
			sql.append(" and STR_TO_DATE(s.create_time,'%Y-%m-%d') >= '" + search_beginDate + "'");
		}
		String search_endDate = (String) params.get("search_endDate");
		if (!StringUtils.isEmpty(search_endDate)) {
			sql.append(" and STR_TO_DATE(s.create_time,'%Y-%m-%d') <= '" + search_endDate + "'");
		}	
		
		
		 
	}
	   
	
	/**       
	* 查询所有
	* @param params
	* @return      
	*/
	public List<Map<String, Object>> selectAll${model.model}(Map<String, Object> params){
		StringBuffer sql = new StringBuffer("SELECT s.* FROM ${model.tableName} s where 1=1 ");
		// 拼接查询条件
		pageWhereSql(sql, params);
		logger.info("sql:" + sql);
		
		// 默认ID倒序排列
		sql.append(" ORDER BY s.create_time desc ");
		
		return jdbcTemplate.queryForList(sql.toString());
	}
	
	      
}